package com.generator.mybatis.generator.core.ganerator;

import com.generator.mybatis.generator.core.config.EntityConfig;
import com.generator.mybatis.generator.core.config.MapperConfig;
import com.generator.mybatis.generator.core.enums.OpenEnum;
import com.generator.mybatis.generator.core.enums.TemplateEnum;
import com.generator.mybatis.generator.util.CreateTemplateUtil;
import com.generator.mybatis.generator.util.WordUtil;
import com.generator.mybatis.generator.core.vo.TableVO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateMapper {
    private String path;
    private MapperConfig mapperConfig;
    private EntityConfig entityConfig;
    public CreateMapper(String path,EntityConfig entityConfig,MapperConfig mapperConfig){
        this.path=path;
        this.entityConfig=entityConfig;
        this.mapperConfig=mapperConfig;
    }
    public void createFile(List<TableVO> tables){
        if(mapperConfig.getOpen().equals(OpenEnum.OFF)){
            return;
        }
        for(TableVO table:tables){
            try {
                String className= WordUtil.getClassName(table.getName())+entityConfig.getSuffix();
                String mapperName=WordUtil.getClassName(table.getName())+mapperConfig.getSuffix();
                String classPath=path+"/"+mapperConfig.getPackagePath().replaceAll("\\.","/")+"/"+mapperName+".java";
                Map<String,Object> params=new HashMap<>();
                params.put("className",className);
                params.put("mapperName",mapperName);
                params.put("entityPath",entityConfig.getPackagePath());
                params.put("mapperPath",mapperConfig.getPackagePath());
                CreateTemplateUtil.createFile(TemplateEnum.mapper.toString(),classPath, params);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
